package com.yangjiangtao.controller;

import com.yangjiangtao.common.User;
import com.yangjiangtao.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public ModelAndView loginCheck(HttpServletRequest request, User loginUser){
        boolean isValidUser = userService.hasMatchUser(loginUser.getUserName(), loginUser.getPassword());
        if (!isValidUser){
            return new ModelAndView("login","error","用户名或密码错误");
        }else{
            User user = userService.findUserByUserName(loginUser.getUserName());
            request.getSession().setAttribute("user",user);
            return new ModelAndView("main");
        }
    }
}
